package com.swak.pool;

import java.util.Queue;
import java.util.function.Supplier;

class PoolImpl<T> implements Pool<T> {

	private final Supplier<T> newFunc;
	private final Queue<T> queue;

	public PoolImpl(Supplier<T> newFunc, Queue<T> queue) {
		this.newFunc = newFunc;
		this.queue = queue;
	}

	@Override
	public T borrow() {
		T res = null;
		if ((res = queue.poll()) != null) {
			return res;
		}
		return newFunc.get();
	}

	@Override
	public void release(T t) {
		queue.offer(t);
	}

	@Override
	public void clear() {
		queue.clear();
	}
}
